package android.slc.base.repository.local.sp;

import android.slc.box.commonlibrary.compat.SlcResourceUtils;
import android.slc.box.db.sp.DpManager;

public class AppConfig extends DpManager {

    private static class Holder {
        private static final AppConfig INSTANCE = new AppConfig();
    }

    public static AppConfig getInstance() {
        return AppConfig.Holder.INSTANCE;
    }

    public static void init(int versionCode, String versionName, boolean isRelease, String currentPName) {
        if (appConfigIsInit) {
            return;
        }
        appConfigIsInit = true;
        getInstance().versionCode = versionCode;
        getInstance().versionName = versionName;
        getInstance().isRelease = isRelease;
        getInstance().dp.putValue("versionCode", getInstance().versionCode, false)
                .putValue("versionName", getInstance().versionName, false)
                .putIfAbsent("isRelease", getInstance().isRelease, false);
        getInstance().dp.putIfAbsent("appNameEnable", false, false);
        getInstance().dp.putIfAbsent("packageNameEnable", false, false);
        getInstance().dp.putIfAbsent("appExeEnable", false, false);
        //填充app名称
        if (getAppNameEnable()) {
            getInstance().dp.putIfAbsent("appName", SlcResourceUtils.getString("app_name"), false);
        } else {
            getInstance().dp.putValue("appName", SlcResourceUtils.getString("app_name"), false);
        }
        //填充包名
        if (getPackageNameEnable()) {
            getInstance().dp.putIfAbsent("packageName", "android.slc" + currentPName, false);
        } else {
            getInstance().dp.putValue("packageName", "android.slc" + currentPName, false);
        }
        //填充启动文件名称（仅windows）
        if (getPackageNameEnable()) {
            getInstance().dp.putIfAbsent("appExeName", currentPName + ".exe");
        } else {
            getInstance().dp.putValue("appExeName", currentPName + ".exe");
        }
    }

    private static boolean appConfigIsInit = false;
    private final static String DEF_PACKAGE_NAME = "android.slc.ssfx";
    private final static String DEF_APP_EXE_NAME = "ssfx.exe";
    private int versionCode;
    private String versionName;
    private boolean isRelease = true;

    private AppConfig() {
        super("app_config");
    }

    public static int getVersionCode() {
        return getInstance().dp.getInt("versionCode", getInstance().versionCode);
    }

    public static String getVersionName() {
        return getInstance().dp.getString("versionName", getInstance().versionName);
    }

    public static boolean isIsRelease() {
        return getInstance().dp.getBoolean("isRelease", getInstance().isRelease);
    }

    public static String getAppName() {
        return getInstance().dp.getString("appName", SlcResourceUtils.getString("app_name"));
    }

    public static boolean getAppNameEnable() {
        return getInstance().dp.getBoolean("appNameEnable", false);
    }

    public static String getPackageName() {
        return getInstance().dp.getString("packageName", DEF_PACKAGE_NAME);
    }

    public static boolean getPackageNameEnable() {
        return getInstance().dp.getBoolean("packageNameEnable", false);
    }

    public static String getAppExeName() {
        return getInstance().dp.getString("appExeName", DEF_APP_EXE_NAME);
    }

    public static boolean getAppExeNameEnable() {
        return getInstance().dp.getBoolean("appExeName", false);
    }

}
